/**
 * Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.common.web.servlet;

import org.swan.bean.MessageBean;

import javax.servlet.http.HttpServletRequest;

import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.koron.util.Constant;
import com.koron.util.Tools;

@Controller
public class LoginAction {
	@RequestMapping("/login.htm")
	public ModelAndView login(HttpServletRequest request) {
		ModelAndView mv = Tools.getView("login.btl");
		return mv;
	}

	@RequestMapping("/login/login.htm")
	@ResponseBody
	public String login(@RequestParam("name") String name, @RequestParam("password") String password,
			HttpServletRequest request) {
		try (SessionFactory factory = new SessionFactory()) {
			return new Gson().toJson(factory.runTask(this, "login", MessageBean.class, name, password, request));
		}
	}

	/**
	 * 注销登录
	 * 
	 * @return
	 */
	@RequestMapping("/logout.htm")
	public ModelAndView logout(HttpServletRequest request) {
		request.getSession().removeAttribute(Constant.USER);
		ModelAndView mv = Tools.getView("login.btl");
		return mv;
	}

}